/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.mach.entity.DrawingCategory;
import org.springblade.mach.entity.DrawingNumberApplication;
import org.springblade.mach.entity.DrawingNumberApplicationDetail;
import org.springblade.mach.entity.EquipmentCategory;
import org.springblade.mach.excel.DrawingNumberApplicationExcel;
import org.springblade.mach.service.IDrawingCategoryService;
import org.springblade.mach.service.IDrawingNumberApplicationDetailService;
import org.springblade.mach.service.IEquipmentCategoryService;
import org.springblade.mach.vo.DrawingNumberApplicationDetailVO;
import org.springblade.mach.vo.DrawingNumberApplicationVO;
import org.springblade.mach.mapper.DrawingNumberApplicationMapper;
import org.springblade.mach.service.IDrawingNumberApplicationService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author LY
 * @since 2021-01-13
 */
@Service
@AllArgsConstructor
public class DrawingNumberApplicationServiceImpl extends BaseServiceImpl<DrawingNumberApplicationMapper, DrawingNumberApplication> implements IDrawingNumberApplicationService {

	private IEquipmentCategoryService equipmentCategoryService;
	private IDrawingCategoryService drawingCategoryService;
	private IDrawingNumberApplicationDetailService drawingNumberApplicationDetailService;


	@Override
	public IPage<DrawingNumberApplicationVO> selectDrawingNumberApplicationPage(IPage<DrawingNumberApplicationVO> page, DrawingNumberApplicationVO drawingNumberApplication) {
		return page.setRecords(baseMapper.selectDrawingNumberApplicationPage(page, drawingNumberApplication));
	}

	@Override
	public List<DrawingNumberApplicationExcel> exportExcel(Wrapper<DrawingNumberApplication> queryWrapper) {
		return baseMapper.exportExcel(queryWrapper);
	}

	/**
	 * 保存申请的图号记录以及图号的详情信息
	 * @param drawingNumberApplication
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveDrawingNumberApplicationAndDetail(DrawingNumberApplication drawingNumberApplication) {
		// 保存图号申请记录
		boolean saveDrawingNumApplication = save(drawingNumberApplication);
		// 保存图号的详情记录
		List<DrawingNumberApplicationDetail> drawingNumberApplicationDetailList = new ArrayList<>();
		// 申请的数量
		Integer number =  drawingNumberApplication.getNum();
		if (number != null && number > 0) {
			// 设备类别
			EquipmentCategory equipmentCategory = equipmentCategoryService.getById(drawingNumberApplication.getProductChildId());
			// 图纸类别
			DrawingCategory drawingCategory = drawingCategoryService.getById(drawingNumberApplication.getDrawingCategory());
			String prefixStr = "MH" + equipmentCategory.getCode() + "." + drawingCategory.getCode() + ".";
			// 数据库中存在得个数(详情表中得记录)
			Integer index =  drawingNumberApplicationDetailService.getCountByParentIdAndChildIdAndCategoryId(
				drawingNumberApplication.getProductParentId(),
				drawingNumberApplication.getProductChildId(),
				drawingNumberApplication.getDrawingCategory());
			for (int i = 0; i < number; i++) {
				String numberIndex;
				if (index != null && index > 0) {
					// 数据库中存在当前详情表中最大得记录
					String maxIndex = drawingNumberApplicationDetailService.getMaxIndexByAppliactionId(
						drawingNumberApplication.getProductParentId(),
						drawingNumberApplication.getProductChildId(),
						drawingNumberApplication.getDrawingCategory());
					// 字符串序列
					numberIndex = prefixStr + StringUtils.leftPad((i + Integer.valueOf(maxIndex) + 1) + "", 5, "0");
				}else {
					numberIndex = prefixStr + StringUtils.leftPad((i + 1) + "", 5, "0");
				}
				// 图号申请详情
				DrawingNumberApplicationDetail drawingNumberApplicationDetail = new DrawingNumberApplicationDetail();
				drawingNumberApplicationDetail.setApplicationId(drawingNumberApplication.getId());
				drawingNumberApplicationDetail.setProductParentId(drawingNumberApplication.getProductParentId());
				drawingNumberApplicationDetail.setProductChildId(drawingNumberApplication.getProductChildId());
				drawingNumberApplicationDetail.setDrawingCategory(drawingNumberApplication.getDrawingCategory());
				drawingNumberApplicationDetail.setNumber(numberIndex);
				drawingNumberApplicationDetail.setStatus(DrawingNumberApplicationDetail.STATUS_OCCUPIED);
				drawingNumberApplicationDetailList.add(drawingNumberApplicationDetail);
			}
			boolean saveDetailList = drawingNumberApplicationDetailService.saveBatch(drawingNumberApplicationDetailList);
			return saveDrawingNumApplication && saveDetailList;
		}else {
			return saveDrawingNumApplication;
		}
	}
	@Override
	public Integer getCountByParentIdAndChildIdAndCategoryId(Long productParentId, Long productChildId, Long drawingCategory) {
		return baseMapper.getCountByParentIdAndChildIdAndCategoryId(productParentId,productChildId,drawingCategory);
	}

	/**
	 * 修改图号申领表 以及详情表中得所有得记录
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateDrawingNumberApplicationAndDetail(DrawingNumberApplication drawingNumberApplication) {
		boolean saveNumberApplication  = saveOrUpdate(drawingNumberApplication);
		List<DrawingNumberApplicationDetail> detailList = drawingNumberApplicationDetailService.getDrawingNumberDetailList(drawingNumberApplication.getId());
        if (detailList.size() > 0) {
        	detailList.stream().forEach(drawingNumberApplicationDetailVO -> {
        		drawingNumberApplicationDetailVO.setDrawingCategory(drawingNumberApplication.getDrawingCategory());
        		drawingNumberApplicationDetailVO.setProductParentId(drawingNumberApplication.getProductParentId());
        		drawingNumberApplicationDetailVO.setProductChildId(drawingNumberApplication.getProductChildId());
			});
			boolean saveDetailList = drawingNumberApplicationDetailService.saveOrUpdateBatch(detailList);
			return  saveNumberApplication && saveDetailList;
		}else{
            return saveNumberApplication;
		}
	}





}
